﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace UserInterface
{
   public delegate void sensorActivated (int tlIndex,int laneIndex);

    [Serializable()]
    public class Pedestrian : MovingObject
    {
        //private bool moveSideWays; // 
        //private TrafficLight objTLPeds;
        /// <summary>
        /// setting the departure and the destination of the pedestrian lane
        /// </summary>
        public Lane departure
        {
            get { return this.depart; }
            set { this.depart = value; }
        }

        Random rdm = new Random();

        public event Move MovePedLane;

        // may be to have event that continue moving from the midpoint
        /// <summary>
        ///  destination lane for pedestrian
        /// </summary>
        public Lane destination
        {
            get { return this.desti; }
            set { this.desti = value; }
        }

        /// <summary>
        /// set the  pedestrian current position.
        /// </summary>
        public Point PedPosition
        {
            get { return this.pos; }
            set { this.pos = value; }
        }

        // check if the pedestrian is on the middle or not..
        /// <summary>
        /// the middle point of the pedestrain Lane, dont know what to do with it for now.
        /// </summary>
        public Boolean midpoint
        {
            get;
            set;
        }

        public int speed
        {
            get
            {
                return this.spd;
            }
            set
            {
                this.spd = value;
            }
        }

        public event sensorActivated sensorPressed;

        public Pedestrian()
        {
        }

        //initializing pedestrian object and inheriting the values from moving object class 
        public Pedestrian(Lane depLane, Lane destLane, Move toWalk, sensorActivated toDo)
        {
            //Point PointP = p;
            //this.objTLPeds = new TrafficLight(strNM, PointP, c); // pedestrian object needs values from traffic light like signal lights this is a helper object 
            //this.pos = p;
            //this.moveSideWays = true;
            this.depart = depLane;
            this.desti = destLane;
            this.pos = depart.start;
            this.spd = 3;
            this.midpoint = false;

            if (toWalk != null)
                MovePedLane += new Move(toWalk);
            if (toDo != null)
            sensorPressed += new sensorActivated(toDo);
        }

        public Pedestrian(Point p, Lane depLane, Lane destLane, int speed, PictureBox pbx)
        {
            Point PointP = p;
            //this.objTLPeds = new TrafficLight(strNM, PointP, c); // pedestrian object needs values from traffic light like signal lights this is a helper object 
            this.pos = p;
            this.depart = depLane;
            this.desti = destLane;
            this.spd = speed;
           // this.graphics = pbx;
        }


        /// <summary>
        /// move pedtrian from starting point to end point
        /// </summary>
        /// <param name="direct"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>

        public void moveped(string id, Point end, Parent parent, bool canWalk)
        {
            //how the pede will move, based on from left to right and will check with the sensor once the sensor class is implemented.

            //    }     N thats why we have define the pedestrian lane to have middlepoint. the car would stop when it gets to certain place and it will start moving agan when pedestrian as pass. as we done have time now Zeal, just implement the pedestrian
            //}      E      W
            //}         S        

            //if (midpoint)
            //{
            //    moveOnMidPoint(id, end, parent);
            //}
            //else MoveOnPedLane(id, end, canWalk);
            //if (this.PedPosition.X < end.X) //&& (check if pedestrian TLPedestrian is green)    TECHNICALLY ONLY X AXIS CAN DO THE JOB NO NEED OF Y AXIS HENCE NO SWITCH CASES REQUIRED 
            //{

            //    int diff = end.X - this.PedPosition.X;
            //    for (int i = 0; i < diff; i++)
            //    { this.pos.X += 1; }
            //    if (canWalk)
            //    { this.pedWalk(this); }
            //    return;
            //}

            //if (this.PedPosition.X >= end.X)    //&& (check if pedestrian TLPedestrian is green)
            //{
            //    int diff = this.PedPosition.X - end.X;
            //    for (int i = 0; i < diff; i++)
            //    { this.pos.X -= 1; }
            //    if (canWalk)
            //    { this.pedWalk(this); }
            //    return;
            //}

            //if (this.PedPosition.Y < end.Y)
            //{
            //    int diff = end.Y - this.PedPosition.Y;
            //    for (int i = 0; i < diff; i++)
            //    { this.pos.Y += 1; }
            //    if (canWalk)
            //    { this.pedWalk(this); }
            //    return;
            //}

            //if (this.PedPosition.Y >= end.Y)
            //{
            //    int diff = this.PedPosition.Y - end.Y;
            //    for (int i = 0; i < diff; i++)
            //    { this.pos.Y -= 1; }
            //    if (canWalk)
            //    { this.pedWalk(this); }
            //    return;
            //}

        }

        public void moveOnMidPoint(string id, Point end, Parent parent)
        {
            if (parent == Parent.Type2)
            {
                switch (id)
                {
                    //case "N1-N3 ": // move from left to right
                    case "NLeft-Right1":
                    case "NLeft-Right2":
                        if (this.PedPosition.X < end.X && this.PedPosition.X + spd < end.X)
                        {
                            if (this.PedPosition.X < 90)
                                this.pos.X += spd;
                            else if (this.PedPosition.X >= 150)
                            {
                                this.pos.X += spd;
                            }
                            return;
                        }

                        this.MovePedLane(this);
                        return;


                    case "NRight-Left1":
                    case "NRight-Left2":
                        if (this.PedPosition.X < end.X && this.PedPosition.X + spd < end.X)
                        {
                            if (this.PedPosition.X < 155)
                                this.pos.X += spd;
                            else if (this.PedPosition.X >= 212)
                            {
                                this.pos.X += spd;
                            }
                            return;
                        }

                        this.MovePedLane(this);
                        return;


                    //case "S1-S2 ": // move from left to right
                    case "SLeft-Right1":
                    case "SLeft-Right2":

                        if (this.PedPosition.X < end.X && this.PedPosition.X + spd < end.X)
                        {
                            if (this.PedPosition.X < 90)
                                this.pos.X += spd;
                            else if (this.PedPosition.X >= 150)
                            {
                                this.pos.X += spd;
                            }
                            return;
                        }

                        this.MovePedLane(this);
                        return;

                    case "SRight-Left1":
                    case "SRight-Left2":

                        if (this.PedPosition.X < end.X && this.PedPosition.X + spd < end.X)
                        {
                            if (this.PedPosition.X < 155)
                                this.pos.X += spd;
                            else if (this.PedPosition.X >= 212)
                            {
                                this.pos.X += spd;
                            }
                            return;
                        }

                        this.MovePedLane(this);
                        return;
                }
            }
        }

        public void MoveOnPedLane(string id, Point end, bool canWalk)
        {
            switch (id)
            {
                case "NLeft":
                    if (this.PedPosition.X < end.X && this.PedPosition.X + spd < end.X)
                    {
                        this.pos.X += spd; return;
                    }

                    else if (canWalk)
                        this.MovePedLane(this);
                    else if (this.PedPosition.X >= this.depart.end.X || this.pos.X + spd > this.depart.start.X)
                    { 
                        PedLane pl = (PedLane)departure;
                        if (pl.activeSensor)
                            sensorPressed(3, 0);
                    }

                    return;
                case "NLeft-Right1":
                case "NLeftExit":

                    if (this.PedPosition.X < end.X && this.PedPosition.X + spd < end.X)
                    {
                        this.pos.X += spd; return;
                    }
                    else if (canWalk)
                     {
                        this.MovePedLane(this);
                     }
                    return;

                case "NRight":
                    if (this.PedPosition.X > end.X && this.PedPosition.X - spd > end.X)
                    {
                        this.pos.X -= spd; return;
                    }

                    else if (canWalk)
                        this.MovePedLane(this);
                    else
                    {
                        PedLane pl = (PedLane)departure;
                        if (pl.activeSensor)
                            sensorPressed(3, 0);
                    }
                    return;
                case "NRight-Left1":
                case "NRightExit":

                    if (this.PedPosition.X > end.X && this.PedPosition.X - spd > end.X)
                    {
                        this.pos.X -= spd; return;
                    }

                    else if (canWalk)
                    {
                        this.MovePedLane(this);
                    }
                    return;

                case "SLeft":
                    if (this.PedPosition.X < end.X && this.PedPosition.X + spd < end.X)
                    {
                        this.pos.X += spd; return;
                    }

                    else if (canWalk)
                        this.MovePedLane(this);
                    else
                    {
                        PedLane pl = (PedLane)departure;
                        if (pl.activeSensor)
                            sensorPressed(2, 1);
                    }
                        return;
                case "SLeft-Right1":
                case "SLeftExit":
                    if (this.PedPosition.X < end.X && this.PedPosition.X + spd < end.X)
                    {
                        this.pos.X += spd; return;
                    }

                    else if (canWalk)
                        this.MovePedLane(this);
                    return;

                case "SRight":
                    if (this.PedPosition.X > end.X && this.PedPosition.X - spd > end.X)
                    {
                        this.pos.X -= spd; return;
                    }
                    else if (canWalk)
                        this.MovePedLane(this);
                    else
                    {
                        PedLane pl = (PedLane)departure;
                        if (pl.activeSensor)
                            sensorPressed(2, 1);
                    }
                        return;
                case "SRight-Left1":
                case "SRightExit":
                    if (this.PedPosition.X > end.X && this.PedPosition.X - spd > end.X)
                    {
                        this.pos.X -= spd; return;
                    }

                    else if (canWalk)
                    {
                        this.MovePedLane(this);
                    }
                    return;              
            }
            return;
        }
    }
}
